<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="检索信息" prop="search">
        <el-input v-model="queryParams.search" placeholder="请输入检索内容" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="性别" prop="opSex">
        <el-select v-model="queryParams.opSex" placeholder="选择性别">
          <el-option label="男" value="0"></el-option>
          <el-option label="女" value="1"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="出生日期" prop="opBirthday">
        <el-date-picker clearable v-model="queryParams.opBirthday" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择出生日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="状态" prop="opStatus">
        <el-select v-model="queryParams.opStatus" placeholder="选择状态">
          <el-option label="正常" value="0"></el-option>
          <el-option label="封禁" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['persion:opeople:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['persion:opeople:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['persion:opeople:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['persion:opeople:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="opeopleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="老人姓名" align="center" prop="opName" />
      <el-table-column label="老人年龄" align="center" prop="opAge">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.opAge > 60">
            {{ scope.row.opAge }}
          </el-tag>
          <el-tag effect="dark" v-else>
            {{ scope.row.opAge }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="opSex">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.opSex == '0'">
            男
          </el-tag>
          <el-tag effect="dark" type="info" v-else-if="scope.row.opSex == '1'">
            女
          </el-tag>
          <el-tag effect="dark" v-else>
            未知
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="籍贯" align="center" prop="opNative" width="180">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger">
            {{ scope.row.opNative }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="照片" align="center" prop="opPhoto">
        <template slot-scope="scope">
          <el-image :src="scope.row.opPhoto" fit="cover" style=" width:50; height:50;">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="opPhone" width="110">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.opPhone != ''">
            {{ scope.row.opPhone }}
          </el-tag>
          <el-tag effect="dark" v-else>
            未知
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="出生日期" align="center" prop="opBirthday" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.opBirthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="登录地点" align="center" prop="loginAddress" />
      <el-table-column label="登录时间" align="center" prop="loginData">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginData, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="opStatus">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.opStatus" active-value="0" inactive-value="1"
            @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态" align="center" prop="opStatus">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.opStatus != '0'">
            封禁
          </el-tag>
          <el-tag effect="dark" v-else>
            正常
          </el-tag>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['persion:opeople:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['persion:opeople:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改老人个人信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="老人姓名" prop="opName">
          <el-input :disabled="true" v-model="form.opName" placeholder="请输入老人姓名" />
        </el-form-item>
        <el-form-item label="老人年龄" prop="opAge">
          <el-input-number :disabled="form.opId != null" v-model="form.opAge" @change="handleChange" :max="100"
            label="请选择年龄"></el-input-number>
        </el-form-item>
        <el-form-item label="籍贯" prop="opNative">
          <el-input :disabled="true" v-model="form.opNative" placeholder="采用orc图片认证" />
        </el-form-item>

        <el-form-item label="头像" prop="opPhoto" v-if="form.opId != null">
          <el-upload :disabled="true" class="upload-demo" v-model="form.opPhoto" list-type="picture">
          </el-upload>
        </el-form-item>
        <el-form-item label="身份证" prop="opCard">
          <el-upload class="upload-demo" v-model="form.opCard" action="/dev-api/manage/older/IDCardOCR"
            :on-success="handleSuccess" :file-list="fileList" list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="身份证号" prop="opIdcard" v-if="form.opId == null">
          <el-input :disabled="true" v-model="form.opIdcard" placeholder="采用orc图片认证" />
        </el-form-item>
        <el-form-item label="联系电话" prop="opPhone">
          <el-input v-model="form.opPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="出生日期" prop="opBirthday">
          <el-date-picker :disabled="true" clearable v-model="form.opBirthday" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="登录ip" prop="loginIp" v-if="form.opId != null">
          <el-input :disabled="true" v-model="form.loginIp" placeholder="请输入最后登录ip" />
        </el-form-item>
        <el-form-item label="登录地点" prop="loginAddress" v-if="form.opId != null">
          <el-input :disabled="true" v-model="form.loginAddress" placeholder="请输入最后登录地点" />
        </el-form-item>
        <el-form-item label="登录时间" prop="loginData" v-if="form.opId != null">
          <el-date-picker :disabled="true" clearable v-model="form.loginData" type="date" value-format="yyyy-MM-dd"
            placeholder="最后登录时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOpeople, getOpeople, delOpeople, addOpeople, updateOpeople, changeUserStatus } from "@/api/manage/older";

export default {
  name: "Opeople",
  data () {
    return {
      fileList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 老人个人信息表格数据
      opeopleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        search: null,
        opBirthday: null,
        opStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        opName: [
          { required: true, message: "老人姓名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    // 上传成功 回调函数
    handleSuccess (response, file, fileList) {
      if (response.code == '500') {
        this.$message.error(response.msg)
        return;
      }
      console.log(response);
      var data = {
        name: response.newFileName,
        url: response.url
      }
      this.fileList.push(data)
      this.form.opCard = response.url
      this.form.opPhoto = response.opPhoto
      this.form.opAge = response.opAge
      this.form.opIdcard = response.IdCardResultVO.idNum
      this.form.opNative = response.IdCardResultVO.address
      this.form.opBirthday = response.IdCardResultVO.birth
      this.form.opName = response.IdCardResultVO.name
      this.form.opAddress = response.IdCardResultVO.address
    },
    handleChange (value) {
      this.form.opAge = value
    },
    // 老人状态修改
    handleStatusChange (row) {
      let text = row.opStatus === "0" ? "启用" : "封禁";
      this.$modal.confirm('确认要"' + text + '""' + row.opName + '"账户吗？').then(function () {
        return changeUserStatus(row.opId, row.opStatus);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.opStatus = row.opStatus === "0" ? "1" : "0";
      });
    },
    /** 查询老人个人信息列表 */
    getList () {
      this.loading = true;
      listOpeople(this.queryParams).then(response => {
        this.opeopleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        opId: null,
        opName: null,
        opAge: null,
        opSex: null,
        opNative: null,
        opAddress: null,
        opPhoto: null,
        opCard: null,
        opPhone: null,
        opBirthday: null,
        delFlag: null,
        loginIp: null,
        loginAddress: null,
        loginData: null,
        opPassword: null,
        opStatus: null
      };
      this.fileList = []
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.opId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加老人个人信息";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const opId = row.opId || this.ids
      getOpeople(opId).then(response => {
        var data = {
          name: response.data.opName,
          url: response.data.opCard
        }
        this.fileList.push(data)
        this.form = response.data;
        this.open = true;
        this.title = "修改老人个人信息";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.opBirthday = new Date(this.form.opBirthday)
          addOpeople(this.form).then(response => {
            this.$modal.msgSuccess(response.msg);
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const opIds = row.opId || this.ids;
      this.$modal.confirm('是否确认删除老人个人信息编号为"' + opIds + '"的数据项？').then(function () {
        return delOpeople(opIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('manage/older/export', {
        ...this.queryParams
      }, `老人信息_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
